Building a CPU from the ground up Part 4 - The Arithmetic Logic Unit

I’m going to recreate the ALU module that circuitverse has built in. Then according to my rules I will be able to use it in the main CPU build. The circuitverse ALU has two 4 bit inputs A and B, a 4 bit output Q and a 3 bit control line CTR. It performs the following functions on A and B depending on what CTR is set as

CTR
Function
000 A&B
001 A|B
010 A+B
011
100 A&B'
101 A|B'
110 A-B
111 A<B

A&B and A|B I already have. These are just the 4 bit AND and 4 bit OR modules.

I’m going to use the modules for A&B and A|B I created to create the modules for A&B’ and A|B’. All I need is a 4 bit NOT gate on the input of B.

Binary adders and subtracters
I’m not going to go into detail about the theory of binary adders here. There is plenty of that on the internet already. I’ve created a single bit binary adder and combined four of them to make a 4 bit binary adder. The adders all have a carry in bit Cin and a carry out bit Cout. The binary subtractor was constructed using the 2’s compliment method: invert one of the inputs, add them then add 1 by setting the carry in bit to 1.

Comparators
The last module for the ALU is a comparator. Basically it outputs 1 if A<B and 0 otherwise. I’ve created a 1 bit comparator that has two outputs for less than and equality. When creating the 4 bit comparator I’ve used 4 1 bit comparators. Each of the 4 bits is compared and for each bit the more significant bits are checked for equality.

Putting it all together to construct the ALU
The inputs to the ALU A and B get fed into all 7 function modules simultaneously. The 3 bit control line is fed into an 8:1 4 bit mux to select which of the functions is passed through to the output.

Comments